package framework
{
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	
	internal function chainInternal(responder:ChainResponder):void
	{
		var chain:Array = responder.chain;

		var func:Object= chain[0];
		var params:Array = chain[1] as Array;
		chain.splice(0,2);
		
		var aToken:AsyncToken;
		if (func is Function)
		{
			aToken = func.apply(this,params) as AsyncToken;
		}
		else if (func is AbstractOperation)
		{
			aToken = func.send.apply(this,params) as AsyncToken;
		}
		else
		{
			throw new Error("Unsupported operation. Only Function or AbstractOperation are supported");
		}
		if (aToken)
		{
			aToken.addResponder(responder);
		}
		else
		{
			if (chain.length) chainInternal(responder);	
		}
	}
}